//
//  MYCommonSettingViewController.m
//  奇闻
//
//  Created by 似水灵修 on 14-12-2.
//  Copyright (c) 2014年 MingYueGuCheng. All rights reserved.
//

#import "MYCommonSettingViewController.h"
#import "MYBaseSetting.h"
#import "MYFontSizeTool.h"
#import "MYFontSizeViewCOntroller.h"
#import "SDImageCache.h"

@interface MYCommonSettingViewController ()
@property (nonatomic, weak) MYSettingItem *fontSizeItem;
@end
@implementation MYCommonSettingViewController
- (void)loadView
{
    [super loadView];
    // 添加第0组
    [self setupGroup0];
    // 添加第1组
    [self setupGroup1];
    // 添加第2组
    [self setupGroup2];
    // 添加第3组
    [self setupGroup3];
    // 添加第4组
    [self setupGroup4];
    [self setupGroup5];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fontSizeChange) name:MYFontSizeKey object:nil];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)fontSizeChange
{
    self.fontSizeItem.subTitle = [MYFontSizeTool fontSize];
    [self.tableView reloadData];
}
- (void)setupGroup0
{
    // 阅读模式
    MYSettingItem *read = [MYSettingItem itemWithTitle:@"阅读模式"];
    read.subTitle = @"有图模式";
    
    // 字体大小
    MYSettingItem *fontSize = [MYSettingItem itemWithTitle:@"字体大小"];
    self.fontSizeItem = fontSize;
    NSString *fontSizeStr = [MYFontSizeTool fontSize];
    if (fontSizeStr) {//有
        fontSize.subTitle = fontSizeStr;
    }
    else {//没有
        fontSize.subTitle = @"小";
        [MYFontSizeTool saveFontSize:self.fontSizeItem.subTitle];
    }
    fontSize.destVCClass = [MYFontSizeViewCOntroller class];
    // 显示备注
    MYSwitchItem *remark = [MYSwitchItem itemWithTitle:@"显示备注"];
    MYGroupItem *group = [[MYGroupItem alloc] init];
    group.items = @[read,fontSize,remark];
    [self.gorups addObject:group];
}
- (void)setupGroup1
{
    // 图片质量
    MYArrowItem *quality = [MYArrowItem itemWithTitle:@"图片质量" ];
    
    MYGroupItem *group = [[MYGroupItem alloc] init];
    group.items = @[quality];
    [self.gorups addObject:group];
}
- (void)setupGroup2{
    // 声音
    MYSwitchItem *sound = [MYSwitchItem itemWithTitle:@"声音" ];
    
    MYGroupItem *group = [[MYGroupItem alloc] init];
    group.items = @[sound];
    [self.gorups addObject:group];
}
- (void)setupGroup3
{
    // 多语言环境
    MYSettingItem *language = [MYSettingItem itemWithTitle:@"多语言环境"];
    language.subTitle = @"跟随系统";
    MYGroupItem *group = [[MYGroupItem alloc] init];
    group.items = @[language];
    [self.gorups addObject:group];
}
- (void)setupGroup4
{
    // 清空图片缓存
    MYArrowItem *clearImage = [MYArrowItem itemWithTitle:@"清空图片缓存"];
    //KB
    CGFloat size = [[SDImageCache sharedImageCache] getSize] / 1024.0;
    NSString *sizeStr = [NSString stringWithFormat:@"%.1fKB",size];
    if (size >= 1024) {
        //MB
        CGFloat floatSize = size / 1024.0;
        sizeStr = [NSString stringWithFormat:@"%.1fMB", floatSize];
    }
    clearImage.subTitle = sizeStr;
    __weak typeof(self) weakSelf = self;
    clearImage.operateBlock = ^ (MYSettingItem *item) {
        //// 清空所有的硬盘缓存
        [[SDImageCache sharedImageCache] clearDisk];
        item.subTitle = @"0.0KB";
        [weakSelf.tableView reloadData];
    };
    MYGroupItem *group = [[MYGroupItem alloc] init];
    group.items = @[clearImage];
    [self.gorups addObject:group];
}

- (void)setupGroup5{
    // 声音
    MYCheakItem *push = [MYCheakItem itemWithTitle:@"本地推送" ];
    __weak typeof(self) weakSelf = self;
    push.operateBlock = ^(MYSettingItem *item){
        MYCheakItem *cheakItem = (MYCheakItem *)item;
        cheakItem.cheak = !cheakItem.isCheak;
        [weakSelf.tableView reloadData];
        if (cheakItem.isCheak) {//添加一条推送
            [weakSelf scheduleLocalNotif];
        }
        else {//移除推送
            [weakSelf removeLocalNotif];
        }
    };
    
    MYGroupItem *group = [[MYGroupItem alloc] init];
    group.items = @[push];
    [self.gorups addObject:group];
}
//开启一条推送
- (void)scheduleLocalNotif
{
    //本地推送
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:3.0];//通知提醒时间 3秒
    localNotif.timeZone = [NSTimeZone defaultTimeZone];//时区
    
    localNotif.alertBody = @"有消息了！";//通知内容
    localNotif.alertAction = @"xxxx";//锁屏显示内容
    localNotif.alertLaunchImage = @"LaunchImage";
    localNotif.applicationIconBadgeNumber = 10;
    //执行本地
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
//移除推送
- (void)removeLocalNotif
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}
@end
